package com.matt.repository.impl;

import com.matt.domain.course.CourseTopic;
import com.matt.foundation.query.CourseTopicQuery;
import com.matt.commons.page.PagerQuery;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.TargetObjectCourseRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/3/9
 * *******************************
 */
@Component
public class TargetObjectCourseRepositoryImpl extends BaseNativeSqlRepository implements TargetObjectCourseRepository{

    @Override
    public List<Object[]> findByCourseTopicPage(CourseTopicQuery query) {
        String querySql = String.format("select c.id,c.topicMonth, c.beginDate, c.endDate, c.name, t.name as tName, (SELECT count(cp.id) from course_plan_topic  cp where cp.topicId=c.id) as courseNum "
                + " from course_topic c LEFT join target_object t on c.targetObjectId = t.id " +
                " WHERE true ");
        if (StringUtils.isNotBlank(query.getAreaId())) {
            querySql += String.format(" and c.areaId='%s' ", query.getAreaId());
        }

        querySql += " order by c.topicMonth desc " ;
        querySql += String.format(" limit %s, %s ", query.getPageNum() * query.getPageSize() ,  query.getPageSize());
        List<Object[]> objectArrayList = sqlArrayList(querySql);
        return objectArrayList;
    }

    @Override
    public Long countByCourseTopicPage(CourseTopicQuery query) {
        String querySql = String.format("select count(c.id) "
                + " from course_topic c  " +
                " WHERE true ");
        if (StringUtils.isNotBlank(query.getAreaId())) {
            querySql += String.format(" and c.areaId='%s' ", query.getAreaId());
        }

        List objectArrayList = sqlSingleList(querySql);
        return Long.valueOf(objectArrayList.get(0).toString());
    }
}
